package com.hnucm.cooklife.presenter.detail;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.DishDetail;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.bean.uploaddish.UpRece;
import com.hnucm.cooklife.contract.dishdetail.Contract;
import com.hnucm.cooklife.model.DishDetailModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class DishDetailPresenter extends BasePresenter<Contract.IDishDetailView> implements Contract.IDishDetailPresenter {
    Contract.IDishDetailModel mIDishDetailModel;

    public DishDetailPresenter() {
        mIDishDetailModel = new DishDetailModel();
    }

    @Override
    public void collect(int userId, int dishId) {
        mIDishDetailModel.collect(userId, dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ToAddCollectData>() {
                    @Override
                    public void onSubscribe(@androidx.annotation.NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@androidx.annotation.NonNull ToAddCollectData collectData) {
                        if(isViewAttached()){
                            getView().onCollect(collectData,dishId);
                        }
                    }

                    @Override
                    public void onError(@androidx.annotation.NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void unCollect(int userId, int dishId) {
        mIDishDetailModel.unCollcet(userId, dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ToAddCollectData>() {
                    @Override
                    public void onSubscribe(@androidx.annotation.NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@androidx.annotation.NonNull ToAddCollectData collectData) {
                        if(isViewAttached()){
                            getView().onUnCollect(collectData,dishId);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@androidx.annotation.NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage());
                        if(isViewAttached()){
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void loadDishDetail(Integer dishId) {
        mIDishDetailModel.loadDishDetail(dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DishDetail>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull DishDetail DishDetailes) {
                        if(isViewAttached()){
                            getView().onLoadDishDetail(DishDetailes);
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
//                            getView().onLoadFailed();
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshDishDetail(Integer dishId) {
        mIDishDetailModel.refreshDishDetail(dishId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DishDetail>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull DishDetail dishDetail) {
                        if(isViewAttached()){
                            getView().onRefreshDishDetail(dishDetail);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
                            getView().onLoadingFailed();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void updateHistory(int dishId, int userId) {
        mIDishDetailModel.updateHistory(dishId, userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UpRece>() {
                    @Override
                    public void onSubscribe(@androidx.annotation.NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@androidx.annotation.NonNull UpRece upRece) {
                        if(isViewAttached()){
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@androidx.annotation.NonNull Throwable e) {
                        e.printStackTrace();
                        System.out.println(e.getMessage()+"updatehis");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
